import { Object3D } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param script javascript object
 * @constructor
 */
class RemoveScriptCommand extends Command {
	private object:Object3D;
	private script:string;
	private index:number|undefined;
	constructor( editor:Editor, object:Object3D, script:string ) {

		super( editor );

		this.type = 'RemoveScriptCommand';
		this.name = 'Remove Script';

		this.object = object;
		this.script = script;
		if ( this.object && this.script ) {

			this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );

		}

	}

	execute() {

		if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;

		if ( this.index!=undefined && this.index !== - 1 ) {

			this.editor.scripts[ this.object.uuid ].splice( this.index, 1 );

		}

		this.editor.signals.scriptRemoved.dispatch( this.script );

	}

	undo() {

		if ( this.editor.scripts[ this.object.uuid ] === undefined ) {

			this.editor.scripts[ this.object.uuid ] = [];

		}
		if ( this.index!=undefined )
		this.editor.scripts[ this.object.uuid ].splice( this.index, 0, this.script );

		this.editor.signals.scriptAdded.dispatch( this.script );

	}


}

export { RemoveScriptCommand };
